package com.ruoyi.business.utils;

import com.ruoyi.business.service.IBizMemberService;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.utils.DictUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeamUtil {

    //团队架构数据
    public static Map getTeamData(IBizMemberService bizMemberService, Long memberID, Long productID)
    {
        Map paramMap = new HashMap();
        paramMap.put("memberID", memberID);
        if (productID != null) {
            paramMap.put("productID", productID);
        }
        List<Map> teamList = bizMemberService.selectTeamData(paramMap);
        Map temp = new HashMap();
        long teamNum = 0;
        //取出架构人员数据
        for (Map item : teamList) {
            Long rID = (Long) item.get("recommend_id");
            List<Map> chList = (List<Map>) temp.get(rID);
            if (chList == null) {
                chList = new ArrayList();
                temp.put(rID, chList);
            }
            chList.add(item);
            teamNum += ((BigDecimal) item.get("num")).longValue();
        }
        //重组数据
        for (Map item : teamList) {
            Long id = (Long) item.get("id");
            List<Map> chList = (List<Map>) temp.get(id);
            if (chList != null) {
                item.put("children", chList);
            }
        }
        //取出团队盒数等级配置
        List<SysDictData> levels = DictUtils.getDictCache("busi_teamaward_level");
        int numLimit = Integer.parseInt(DictUtils.getDictLabel("busi_award_set", "1"));
        long teamDou = TeamUtil.getTeamDou(teamNum, levels, numLimit);
        //归总直属下级盒数
        List<Map> list = (List<Map>) temp.get(memberID);
        if (list != null) {
            for (Map item : list) {
                Long id = (Long) item.get("id");
                long num = ((BigDecimal) item.get("num")).longValue();
                item.put("totalNum", num);
                getTeamNum((List<Map>) temp.get(id), item);
                item.put("desc", getTeamDesc((Long) item.get("totalNum"), levels, numLimit, teamDou));
            }
        }
        Map resultMap = new HashMap();
        resultMap.put("teamNum", teamNum);
        resultMap.put("memberList", list);
        return resultMap;
    }

    //取出子级团队盒数
    private static void getTeamNum(List<Map> chList, Map parent)
    {
        if (chList == null) return;
        for (Map item : chList) {
            long num = ((BigDecimal) item.get("num")).longValue();
            long totalNum = (Long) parent.get("totalNum");
            parent.put("totalNum", totalNum + num);
            List<Map> children = (List<Map>) item.get("children");
            if (children != null) {
                getTeamNum(children, parent);
            }
        }
    }

    //取出团队盒数说明
    private static String getTeamDesc(long totalNum, List<SysDictData> levels, int numLimit, long teamDou)
    {
        long dou = TeamUtil.getTeamDou(totalNum, levels, numLimit);
        if (teamDou == 0) {
            return "[团队盒数" + totalNum + " 无分成]";
        }
        return "[团队盒数" + totalNum + " 分成" + teamDou + "-" + dou + "=" + (teamDou - dou) + "福豆]";
    }

    //取得当前团队盒数对应分成
    public static long getTeamDou(long totalNum, List<SysDictData> levels, int numLimit)
    {
        if (totalNum <= numLimit) {
            return 0L;
        }
        for (SysDictData data : levels) {
            String label = data.getDictLabel();
            long dou = Long.parseLong(data.getDictValue());
            String[] split = label.split("-");
            long begin = Long.parseLong(split[0]);
            long end = Long.parseLong(split[1]);
            if (totalNum >= begin && totalNum <= end) {
                return dou;
            }
        }
        return 0L;
    }

    //测试all包含字符串
    public static boolean testContains(String allID, Long id)
    {
        return ("," + allID + ",").indexOf("," + id + ",") >= 0;
    }
}
